<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page language="java" import="java.sql.*"%>
<%
	Class.forName("org.postgresql.Driver");
	Connection conn = DriverManager.getConnection(
			"jdbc:postgresql://localhost:5432/cse132b", "cse132blogin",
			"1234567890");
	Statement stmt, stmt1, stmt2;

	if (request.getParameter("ACTION") != null) {
		if (request.getParameter("ACTION").equals("Insert")) {
			int NewID = 0;

			PreparedStatement statement = conn
					.prepareStatement("INSERT INTO departments(name,abbreviation) VALUES (?,?)");
			statement.setString(1, request.getParameter("name"));
			statement.setString(2, request.getParameter("abbreviation"));

			statement.executeUpdate();
			statement.close();

		}

		if (request.getParameter("ACTION").equals("Update")) {
			int EditID = Integer.valueOf(request
					.getParameter("PARAMETER"));

			PreparedStatement statement = conn
					.prepareStatement("UPDATE departments SET name = ?, abbreviation = ? WHERE department_id = ?");

			statement.setString(
					1,
					request.getParameter("name"
							+ String.valueOf(EditID)));
			statement.setString(
					2,
					request.getParameter("abbreviation"
							+ String.valueOf(EditID)));
			
			statement.setInt(3, EditID);
			statement.executeUpdate();
			statement.close();
		}

		if (request.getParameter("ACTION").equals("Delete")) {
			int EditID = Integer.valueOf(request
					.getParameter("PARAMETER"));
			
			PreparedStatement statement = conn
					.prepareStatement("DELETE FROM departments WHERE department_id = ?");
			statement.setInt(1, EditID);
			statement.executeUpdate();
			statement.close();
		}
	}
%>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
	function Insert() {
		document.getElementById("name").required = true;
		document.getElementById("ACTION").value = "Insert";
	}
	function Update(row) {
		document.getElementById("name"+row).required = true;
		document.getElementById("ACTION").value = "Update";
		document.getElementById("PARAMETER").value = row;
	}
	function Delete(row) {
		document.getElementById("ACTION").value = "Delete";
		document.getElementById("PARAMETER").value = row;
	}
</script>
</head>
<body>
	<table border="1">
		<tr>
			<td valign="top" width="40%">
				<%-- -------- Include menu HTML code -------- --%> <jsp:include
					page="/menu.html" />
			</td>
			<td>
				<form id="myForm" method="post" action="departments.jsp">
					<p align="center">
						<font size="6">Review Session</font>
					</p>
					<table border="1">
						<tr>
							<th>Name</th>
						</tr>
						<%---------- Add ----------%>
						<tr>
							<td><input type="text" name="name" id="name"/></td>
							<td><input type="text" name="abbreviation" id="abbreviation"/></td>
							<td align="center"><input type="submit" value="Insert"
								onclick="Insert()"></td>
						</tr>
						<%---------- Edit ----------%>
						<%
							stmt = conn.createStatement();
							ResultSet rsDepartments = stmt
									.executeQuery("SELECT * FROM departments");

							while (rsDepartments.next()) {
						%>
						<tr>
							<td align="center"><%=rsDepartments.getInt("department_id")%></td>
							<td><input type="text"
								name="name<%=rsDepartments.getInt("department_id")%>"
								id="name<%=rsDepartments.getInt("department_id")%>"
								value="<%=rsDepartments.getString("name")%>"></td>
							<td><input type="text"
								name="abbreviation<%=rsDepartments.getInt("department_id")%>"
								id="abbreviation<%=rsDepartments.getInt("department_id")%>"
								value="<%=rsDepartments.getString("abbreviation")%>"></td>
							<td><input type="submit" value="Update"
								onclick="Update(<%=rsDepartments.getInt("department_id")%>)"> <input
								type="submit" value="Delete"
								onclick="Delete(<%=rsDepartments.getInt("department_id")%>)"></td>
						</tr>
						<%
							}
						%>
					</table>
					<input type="hidden" id="ACTION" name="ACTION" value="None">
					<input type="hidden" id="PARAMETER" name="PARAMETER" value="">
				</form>
			</td>
		</tr>
	</table>
</body>
</html>
<%
	// Close the Connection
	conn.close();
%>